<?php
include_once 'Controller/IController.php';
include_once 'DBHelper.php';
class BaseController implements IController {
	protected $_action;
	/**
	 *
	 * @var DBHelper
	 */
	protected $_db;
	protected $_class;
	protected $_id;
	protected $_sql;
	public function __construct() {
		$this->init ();
	}
	public function init() {
		$this->_db = new DBHelper ();
		if (isset ( $_GET ['_module'] )) {
			$this->_class = strtolower ( $_GET ['_module'] );
		}
		if (isset ( $_GET ['_id'] )) {
			$this->_id = strtolower ( $_GET ['_id'] );
		}
		if (isset ( $_GET ['_action'] ))
			$this->_action = $_GET ['_action'];
		switch ($this->_action) {
			case 'getAll' :
				$this->getAll ();
				break;
			case 'getById' :
				$this->getById ();
				break;
			case 'delete' :
				$this->delete ();
				break;
			case 'iOrU' :
				$this->iOrU ();
				break;
			default :
				;
				break;
		}
	}
	/*
	 * (non-PHPdoc) @see IController::run()
	 */
	public function run() {
		// TODO Auto-generated method stub
	}
	public function getAll() {
		$sql = 'select * from ' . $this->_class;
		$result = $this->_db->fetchAll ( $sql );
		print_r ( $result );
	}
	public function getSQL() {
	}
	public function getData() {
	}
	public function iOrU() {
		if ($this->_id == NULL)
			$sql = $this->getSQL ()[0];
		else
			$sql = $this->getSQL ()[1];
		$st = $this->_db->_db->prepare ( $sql );
// 		echo $sql;
		$this->_db->iOrU ( $st, $this->getData () );
	}
	public function getById() {
		$sql = 'select * from ' . $this->_class . ' where id = ' . $this->_id;
		$result = $this->_db->fetchAll ( $sql );
		if (count ( $result ) == 1) {
			$result = $result [0];
		}
		print_r ( $result );
	}
	public function delete() {
		$sql = 'delete from ' . $this->_class . ' where id = ' . $this->_id;
		$result = $this->_db->delete ( $sql );
		echo $result;
	}
}
?>